//
//  UIButton+Associated.m
//  AssociatedTest
//
//  Created by 崔宝印 on 2018/5/3.
//  Copyright © 2018年 Koolearn. All rights reserved.
//

#import "UIButton+Associated.h"
#import <objc/runtime.h>

static char *const kButtonClickedKey  = "ButtonClickedKey";

@implementation UIButton (Associated)

- (void)setButtonClicked:(ButtonClicked)clickedBlock
{
    objc_setAssociatedObject(self, kButtonClickedKey, clickedBlock, OBJC_ASSOCIATION_COPY);
    [self addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClicked
{
    ButtonClicked clickblock = objc_getAssociatedObject(self, kButtonClickedKey);
    if (clickblock != nil)
    {
        clickblock(self);
    }
}
@end
